<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/login.css">
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-sm">

            </div>
            <div class="col-sm">
                <div class="card" style="width: 30rem;">
                    <div class="card-body shadow">
                        <form action="/index" method="POST">
                            <h2 class="card-title" style="text-align: center; margin-top: 1rem; margin-bottom: 1rem;">登录</h2>
                            <div class="form-group row">
                                <label for="username" class="col-sm-2 col-form-label">用户名</label>
                                <div class="col-sm-10">
                                  <input type="email" class="form-control" id="username" name="username">
                                </div>
                              </div>
                              <div class="form-group row">
                                <label for="inputPassword3" class="col-sm-2 col-form-label">密码</label>
                                <div class="col-sm-10">
                                    <!-- <i class="bi bi-file-lock2"></i> -->
                                  <input type="password" class="form-control" id="inputPassword3">
                                </div>
                              </div>
                            <div class="form-group form-check">
                                <input type="checkbox" class="form-check-input" id="exampleCheck1">
                                <label class="form-check-label" for="exampleCheck1">勿忘我</label>
                            </div>
                            <div class="form-group btn">
                                <button type="button" class="btn btn-success" onclick="jump()">注册</button>
                                <button type="submit" class="btn btn-success" onclick="jumpTo()">登录</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col-sm">

            </div>
        </div>
    </div>
    <!-- <script>
        function jump(){
            window.location.href="./register.html";
        }
    </script> -->
    <script src="/static/js/jquery-3.5.1.min.js"></script>
    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
    <script src="/static/js/bootstrap.min.js"></script>

    <script>
        function register(){
            window.location.href='/register';
        }
        function jump() {
            // 获取表单数据
            let uid = $('#username').val();
            let pwd = $('#password').val();

            // 判断用户名、密码是否为空，为空则提示；不为空则向服务器提交
            if (uid.length > 0 && pwd.length > 0) {
                axios({
                    method: 'post',
                    url: '/loginDone',
                    data: $('#loginForm').serialize(),
                }).then(function (res) {
                    // axios工具下，服务器真正返回的数据在返回对象的data中
                    res = res.data;
                    if (res.code === 200) {
                        window.location.href = '/';
                    } else {
                        alert_fn(res.msg);
                        console.log(res.msg);
                    }
                }).catch((err) => {
                    alert_fn(err);
                    console.log(err);
                });
            } else {
                alert_fn('用户名、密码不能为空');
            }
        }

        // 包装了显示（以及自动隐藏）消息警告的动作
        function alert_fn(msg){
            $('#msgId').text(msg);
            $('.alert').removeClass('invisible');
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }
    </script>

</body>

</html>